package org.redirect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lucky_common.domain.InviteCode;
import org.lucky_common.domain.InviteParam;
import org.lucky_common.domain.LuckyShare;
import org.lucky_common.domain.ParseParamObj;
import org.lucky_common.util.AjaxResult;
import org.redirect.mapper.InviteCodeMapper;
import org.redirect.mapper.InviteMapper;
import org.redirect.mapper.LuckyMapper;
import org.redirect.service.ILuckyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LuckyServiceImpl extends ServiceImpl<LuckyMapper, LuckyShare> implements ILuckyService {

    @Resource
    private LuckyMapper luckyMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private InviteCodeMapper inviteCodeMapper;

    @Resource
    private InviteMapper inviteMapper;

    @Value("${redirect.url}")
    private String url;

    @Override
    public AjaxResult redirect(ParseParamObj parseParamObj) {
        if (parseParamObj == null){
            return AjaxResult.error("无效链接",2010);
        }else {
            LuckyShare inviteCode = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", parseParamObj.getRef()));
            Boolean aBoolean = redisTemplate.hasKey("share:" + parseParamObj.getRef());
            if (inviteCode != null && Boolean.TRUE.equals(aBoolean)){
                return AjaxResult.success(url);
            }
        }
        return AjaxResult.error("无效链接",2010);
    }

    @Override
    public AjaxResult redirectByCode(ParseParamObj parseParamObj) {
        if (parseParamObj == null){
            return AjaxResult.error("无效链接",2010);
        }

        if (parseParamObj.getRef() != null){
            LuckyShare inviteCode2 = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode2", parseParamObj.getRef()));
            if (inviteCode2 != null){
                return AjaxResult.success(inviteCode2);
            }
        }

        return AjaxResult.error("无效链接",2010);
    }

    @Override
    public LuckyShare selectOneWithInvite(String inviteCode) {
        return luckyMapper.selectOneWithInvite(inviteCode);
    }

    @Override
    public AjaxResult redirectByCode2(ParseParamObj parseParamObj) {
        //先判断参数是否为空
        if (parseParamObj == null || parseParamObj.getRef() == null){
            return AjaxResult.error("无效链接",2010);
        }

        //先通过唯一标识自带的动态码查询
        LuckyShare luckyShare = luckyMapper.selectOneWithInvite(parseParamObj.getRef());
        if (luckyShare == null){
            //如果查不到再去动态码数据库查询相应的唯一标识
            InviteCode inviteCode2 = inviteCodeMapper.selectOne(new QueryWrapper<InviteCode>().eq("inviteCode2", parseParamObj.getRef()));
            if (inviteCode2 != null){
                System.out.println(inviteCode2.getLuckyShareId());
                if (inviteCode2.getLuckyShareId() != null){
                    luckyShare = luckyMapper.selectById(inviteCode2.getLuckyShareId());
                }else {
                    System.out.println(inviteCode2.getInviteId());
                    luckyShare = luckyMapper.selectById(inviteCode2.getInviteId());
                }
                LuckyShare luckyShare1 = luckyMapper.selectOneWithInvite(luckyShare.getInviteCode());
                if (luckyShare1 != null){
                    luckyShare1.setUrl(url);
                    if (luckyShare1.getVerifyCount() >= 3 && luckyShare1.isSecondStage() && luckyShare1.isSubmitParam()){
                        InviteParam invitedCode = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", luckyShare1.getInviteCode()));
                        invitedCode.setClickCount(invitedCode.getClickCount() + 1);
                        inviteMapper.updateById(invitedCode);
                    }else {
                        luckyShare.setClickCount(luckyShare.getClickCount() + 1);
                        luckyMapper.updateById(luckyShare);
                    }
                    return AjaxResult.success(luckyShare1);
                }else {
                    return AjaxResult.error("无效链接",2010);
                }
            }else {
                return AjaxResult.error("无效链接",2010);
            }
        }else {
            luckyShare.setUrl(url);
            if (luckyShare.getVerifyCount() >= 3 && luckyShare.isSecondStage() && luckyShare.isSubmitParam()){
                InviteParam invitedCode = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", luckyShare.getInviteCode()));
                invitedCode.setClickCount(invitedCode.getClickCount() + 1);
                inviteMapper.updateById(invitedCode);
            }else {
                luckyShare.setClickCount(luckyShare.getClickCount() + 1);
                luckyMapper.updateById(luckyShare);
            }
            return AjaxResult.success(luckyShare);
        }
    }
}
